package com.cfpl.admin_service.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_service.mapper.FertilizerAcquireRecordMapper;
import com.cfpl.admin_service.service.FertilizerService;
import com.cfpl.project_common.context.BaseContext;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Fertilizer;
import com.cfpl.admin_service.mapper.FertilizerMapper;
import com.cfpl.project_common.pojo.FertilizerAcquireRecord;
import com.cfpl.project_common.pojo.FertilizerUsageRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 14720
* @description 针对表【fertilizer(肥料信息表)】的数据库操作Service实现
* @createDate 2023-10-17 14:29:56
*/
@Service
public class FertilizerServiceImpl extends ServiceImpl<FertilizerMapper, Fertilizer>
    implements FertilizerService {

    @Autowired
    private FertilizerMapper fertilizerMapper;
    @Autowired
    private FertilizerAcquireRecordMapper fertilizerAcquireRecordMapper;
    /**
     * 新增肥料
     * @param fertilizer 新增对象
     */
    @Override
    public void insertFertilizer(Fertilizer fertilizer) {
        //肥料参数信息必须完整
        if (StrUtil.isBlank(fertilizer.getFertilizerName()) || StrUtil.isBlank(fertilizer.getFertilizerName())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        //库存和肥效期必须非空且有值
        if (fertilizer.getFertilizerStock() == null || fertilizer.getFertilizerStock() == 0
                || fertilizer.getFertilizerExpire() == null || fertilizer.getFertilizerExpire() == 0){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        fertilizerMapper.insert(fertilizer);
    }

    /**
     * 根据id查询
     * @param fertilizerId id
     * @return 肥料信息
     */
    @Override
    public Fertilizer queryFertilizerById(Long fertilizerId) {
        return fertilizerMapper.selectById(fertilizerId);
    }

    /**
     * 更新肥料信息
     * @param fertilizer fertilizer
     */
    @Override
    public void updateFertilizer(Fertilizer fertilizer) {
        if (fertilizer.getId() == null || fertilizer.getId() == 0){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        //肥料参数信息必须完整
        if (StrUtil.isBlank(fertilizer.getFertilizerName()) || StrUtil.isBlank(fertilizer.getFertilizerName())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        //库存和肥效期必须非空且有值
        if (fertilizer.getFertilizerStock() == null || fertilizer.getFertilizerStock() == 0
                || fertilizer.getFertilizerExpire() == null || fertilizer.getFertilizerExpire() == 0){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        int id = fertilizerMapper.updateById(fertilizer);

    }

    /**
     * 删除肥料信息
     * @param fertilizerIds 肥料编号集合
     */
    @Override
    public void removeFertilizer(List<Long> fertilizerIds) {
        if (fertilizerIds == null || fertilizerIds.size() < 1){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        fertilizerMapper.removeFertilizer(fertilizerIds);
    }

    /**
     * 添加领取肥料记录
     * @param fertilizerAcquireRecord 记录信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAcquireFertilizerRecord(FertilizerAcquireRecord fertilizerAcquireRecord) {
        //领取肥料编号不能为空且领取数量必须大于零
        if (fertilizerAcquireRecord.getAcquireQuantity() < 1){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
            fertilizerAcquireRecord.setAcquireDate(new Date());
            fertilizerAcquireRecord.setAdminId(BaseContext.getCurrentUserId());
        //先减库存，再去添加领取记录
        boolean stock = fertilizerMapper.deductFertilizerStock(fertilizerAcquireRecord.getFertilizerId(),
                fertilizerAcquireRecord.getAcquireQuantity());
        if (!stock){
            throw new BusException(CodeEnum.INSUFFICIENT_STOCK_ERROR);
        }

        fertilizerMapper.addAcquireFertilizerRecord(fertilizerAcquireRecord);

    }

    /**
     * 添加肥料使用记录
     * @param fertilizerUsageRecords 记录对象
     */
    @Override
    @Transactional
    public void insertFertilizerUsageRecord(FertilizerUsageRecords fertilizerUsageRecords) {

        //设置时间和当前培育人员
        fertilizerUsageRecords.setFertilizationTime(new Date());
        fertilizerUsageRecords.setAdminId(BaseContext.getCurrentUserId());
        //更新领取肥料记录   如果使用的肥料数量将要>领取的肥料数量，那么不执行更新
        boolean flag = fertilizerAcquireRecordMapper.updateFertilizerUsage(fertilizerUsageRecords);
        System.out.println("更新领取肥料记录："+flag);
        //添加肥料使用记录
        if (flag){
            fertilizerMapper.insertFertilizerUsageRecord(fertilizerUsageRecords);
        }else {
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
    }


}




